package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import conference.Conference;
import papers.Manuscript;
import papers.Manuscript.Status;

/**
 * A class that display all the manuscript status.
 * @author Dickson Kwong
 * @version v1.2 Nov. 30, 2011
 */

@SuppressWarnings("serial")
public class PCResultPanel extends JPanel  {

	/**
	 * The list of manuscripts.
	 */
	private List<Manuscript> my_manuscripts;

	/**
	 * The JTable object.
	 */
	private JTable my_table;

	/**
	 * The manuscript object.
	 */
	private Manuscript my_manu;

	/**
	 * The col of the Jable.
	 */
	private int my_row;

	/**
	 * The boolean for the JTable enable status.
	 */
	private boolean psfRun;
	  
	/**
	 * The PCResultPanel Constructor. It creates a panel list all
	 * the status & result of the manuscripts.
	 * @param the_conference
	 */
	public PCResultPanel(Conference the_conference) {
	    super(true);
	    my_manuscripts = the_conference.getManuscripts();
	    setSize(800, 600);
	    setLayout(new BorderLayout());
	    genTable(the_conference);
	    psfRun = true;
	}

	/**
	 * It generates a JTable that contains all the manuscripts
	 * info and status, and allows user to change status as well
	 * as add reviewers to the manuscript.
	 * @param the_conference
	 */
	private void genTable(final Conference the_conference){
		/** *The columns topic of the JTable*/
        final String columnNames[] = {"", "Manuscript", "Author", 
        		"# of Reviews", "Decision Status", 
        		"Set Status", "Add Reviewer"};
        
        /** *The data from the manuscript*/
        final String data[][] = new String[my_manuscripts.size()][7];
    	for (int i = 0; i < my_manuscripts.size(); i++){
        	data[i][0] = Integer.toString(i);
        	data[i][1] = my_manuscripts.get(i).getPaperName();
        	data[i][2] = my_manuscripts.get(i).getAuthor().getName();
        	data[i][3] = Integer.toString(my_manuscripts.get(i).
		    		getReviews().size()) + "/" + Integer.toString(my_manuscripts.get(i).
		    		getReviewers().size());
        	data[i][4] = my_manuscripts.get(i).getStatus().toString();
        	data[i][5] = "Set Status";
        	data[i][6] = "Add Reviewer";
        }
	    
    	/** *Put the table data in to my_table*/
        my_table = new JTable(data, columnNames){
        	  public Component prepareRenderer
        	   (TableCellRenderer renderer, int index_row, int index_col){
	        	  Component comp = super.prepareRenderer(renderer, index_row, index_col);
	        	  	comp.setFont(new Font("Serif", Font.PLAIN, 20));
	        	  	if((index_col == 4 || index_col == 5)
		        			  && !isCellSelected(index_row, index_col)){
		        	  comp.setBackground(Color.lightGray);
		        	  comp.setFont(new Font("Serif", Font.BOLD, 20));
		        	  }		        	  
		        	  else{
		        	  comp.setBackground(Color.white);
		        	  }
	        	  return comp;
	        	  }
    	  };

	      my_table.setRowHeight(30);
	      my_table.setAutoCreateRowSorter(true);

	      final TableColumn my_column = my_table.getColumnModel().getColumn(0);
	        
	      my_table.removeColumn(my_column);
	      my_table.removeEditor();
			
	      my_table.addMouseListener(new MouseAdapter()   
	      {  
    	  		public void mouseClicked(MouseEvent evt)  
		        {  
		        	int col = my_table.getSelectedColumn();
			        int row = my_table.getSelectedRow();
			        
			        try {
			        	col = my_table.convertColumnIndexToModel(col);
				        row = my_table.convertRowIndexToModel(row);	
			        } catch (IndexOutOfBoundsException e) {
			        
			        }
			          
			        my_table.addColumn(my_column);
			        my_row = row;
			        
			        String function = (String)my_table.getModel().getValueAt(row, col);

			        try {
				        if(function  == "Set Status"){
					        my_manu = my_manuscripts.get(Integer
			        				.parseInt((String)my_table.getModel().getValueAt(row, 0)));
					        my_table.setEnabled(false);
					        if(psfRun){
					        	psfRun = false;
						        PCChangeStatusFrame psf = new PCChangeStatusFrame(my_manu, the_conference);
						        psf.addWindowListener(new WindowAdapter() {
						            public void windowClosing(WindowEvent e) {
						            	updateManuStatus();
						              }
						            public void windowDeactivated(WindowEvent e) {
						            	updateManuStatus();
						            }
						            private void updateManuStatus(){
						            	my_table.setEnabled(true);
						            	psfRun = true;
						            	if(my_manu.getStatus() == Status.YES)				        
								        	my_table.setValueAt("Yes", my_row, 3);
								        else if(my_manu.getStatus() == Status.NO)				        
								        	my_table.setValueAt("No", my_row, 3);
								        else
								        	my_table.setValueAt("Undecided", my_row, 3);
						            }
						            });
					        }
					      				        
				        }
				        else if(function  == "Add Reviewer"){
					        my_manu = my_manuscripts.get(Integer
			        				.parseInt((String)my_table.getModel().getValueAt(row, 0)));
					        my_table.setEnabled(false);
					        if(psfRun){
					        	psfRun = false;
					        	PCAddReviewerFrame paf = new PCAddReviewerFrame(my_manu, the_conference);
						        paf.addWindowListener(new WindowAdapter() {
						            public void windowClosing(WindowEvent e) {
						            	updateReviewCount();
						              }
						            public void windowDeactivated(WindowEvent e) {
						            	updateReviewCount();
						            }
						            private void updateReviewCount(){
						            	my_table.setEnabled(true);
						            	psfRun = true;
						            	my_table.setValueAt(Integer.toString(my_manu.
									    		getReviews().size()) + "/" + Integer.toString(my_manu.
											    		getReviewers().size()), my_row, 2);
						            }
					            });
					        }
				        }	
			        } catch (IndexOutOfBoundsException e) {
			        }
			        my_table.removeColumn(my_column);
		        }  
	        });  
	        
	      my_table.setCellSelectionEnabled(false);
	      my_table.setColumnSelectionAllowed(false);
	      my_table.setRowSelectionAllowed(false);
	      my_table.convertColumnIndexToView(0);
	        
		  JScrollPane tableContainer = new JScrollPane(my_table);
	      add(tableContainer);	        
	      my_table.getModel().addTableModelListener(my_table);
	}
}
